/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.uc.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.uc.domain.req.*;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.xy.biz.uc.domain.resp.GetSignatureResponse;
import com.xy.biz.uc.domain.query.SignatureQuery;
import com.xy.biz.uc.domain.converter.SignatureConverter;
import com.xy.biz.uc.client.SignatureClient;
import com.xy.biz.uc.service.SignatureService;

/**
 *
 *
 * @author yzh
 * @date 2021-2-22
 */
@Slf4j
@RestController
@RequestMapping("/api/uc/signature")
public class SignatureController implements SignatureClient {

    @Autowired
    private SignatureService signatureService;

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    public Result<?> saveSignature(@RequestBody SaveSignatureRequest request) {
        return signatureService.saveSignature(SignatureConverter.toBO(request));
    }

    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllSignature(@RequestBody Collection<SaveSignatureRequest> collection) {
        signatureService.saveAllSignature(SignatureConverter.toBO(collection));
        return Result.success();
    }

    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update")
    public Result<?> updateSignature(@RequestBody SaveSignatureRequest request) {
        signatureService.updateSignature(SignatureConverter.toBO(request));
        return Result.success();
    }

    @PostMapping(value = "/upload")
    public Result<?> uploadSignature(@RequestBody UploadSignatureRequest request) {
        return signatureService.uploadSignature(request);
    }

    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteSignature(@RequestBody GetSignatureRequest request) {
        signatureService.deleteSignature(request.getSignatureId());
        return Result.success();
    }

    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetSignatureResponse> getSignature(@RequestBody GetSignatureRequest request) {
        return Result.success(SignatureConverter.toResponse(signatureService.getSignature(request.getSignatureId())));
    }

    @Validatable
    @PostMapping(value = "/get_signature")
    @Override
    public Result<GetSignatureResponse> getSignatureByUserId() {
        return Result.success(SignatureConverter.toResponse(signatureService.getSignatureByUserId()));
    }

    @Validatable
    @PostMapping(value = "/get_signature_count")
    public Result<?> getSignatureCount(@RequestBody GetSignatureByUserIdRequest request) {
        SignatureQuery signatureQuery = SignatureConverter.toQuery(request);
        return Result.success(signatureService.countSignature(signatureQuery));
    }

    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetSignatureResponse>> listSignatureById(@RequestBody Collection<Long> collection) {
        return Result.success(SignatureConverter.toResponse(signatureService.listSignatureById(collection)));
    }

    /**
     * 查找多个
     *
     * @param collection 标识集合
     * @return
     */
    @Override
    public Result<List<GetSignatureResponse>> listSignatureByUserId(Collection<Long> collection) {
        return Result.success(SignatureConverter.toResponse(signatureService.listSignatureByUserId(collection)));
    }

    @PostMapping(value = "/list")
    @Override
    public Result<List<GetSignatureResponse>> listSignature(@RequestBody ListSignatureRequest request) {
        return Result.success(SignatureConverter.toResponse(signatureService.listSignature(SignatureConverter.toQuery(request))));
    }

    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countSignature(@RequestBody ListSignatureRequest request) {
        return Result.success(signatureService.countSignature(SignatureConverter.toQuery(request)));
    }

    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetSignatureResponse>> pageSignature(@RequestBody ListSignatureRequest request) {
        SignatureQuery query = SignatureConverter.toQuery(request);
        List<GetSignatureResponse> list = SignatureConverter.toResponse(signatureService.listSignature(query));
        int count = signatureService.countSignature(query);
        return Result.success(new Page<>(list, count));
    }

}
